package com.qs.eggs.view.recharge.fragment;


import android.content.Intent;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import com.Zxing.Demo.CaptureActivity;
import com.google.gson.Gson;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.qs.eggs.Constants;
import com.qs.eggs.R;
import com.qs.eggs.base.fragment.BaseFragment;
import com.qs.eggs.view.activity.SearchActivity;
import com.qs.eggs.view.activity.SubmitLoginActivity;
import com.squareup.picasso.Picasso;

import org.json.JSONArray;
import org.json.JSONException;

import java.util.HashMap;

import okhttp3.Call;
import okhttp3.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class OnlineFragment extends BaseFragment implements View.OnClickListener{
    private RadioButton rb1,rb2,rb3,rb4,zhifubao,weixin;
    private Button pay;
    private EditText jine;
    private int ids,id=1;
    private String orderMoney;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_online;
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        rb1=findView(R.id.sporttery_football_all_check);
        rb2=findView(R.id.sporttery_football_low_check);
        rb3=findView(R.id.sporttery_football_middle_check);
        rb4=findView(R.id.sporttery_football_high_check);
        zhifubao=findView(R.id.rb0);
        weixin=findView(R.id.rb1);
        jine=findView(R.id.jine);
        pay=findView(R.id.ok);
        pay.setOnClickListener(this);
      if (rb1.isChecked()) {
          ids = 1;
          orderMoney = "100";
          jine.setText("");
      }

       if (rb2.isChecked()){
                    ids=2;
                    orderMoney="300";
                    jine.setText("");
        }



        if (rb3.isChecked()){
                    ids=3;
                    orderMoney="500";
                    jine.setText("");
         }


        if (rb4.isChecked()){
                    ids=4;
                    orderMoney="1000";
                    jine.setText("");
         }

      jine.addTextChangedListener(new TextWatcher() {

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.length() > 0) {
                    rb1.setChecked(false);
                    rb2.setChecked(false);
                    rb3.setChecked(false);
                    rb4.setChecked(false);
                    ids=5;
                    orderMoney=jine.getText().toString();
                } else {

                }
            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void afterTextChanged(Editable s) {
            }
        });
       zhifubao.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               if (isChecked){
                   weixin.setChecked(false);
                   id=1;
               }
           }
       });
        weixin.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked){
                    zhifubao.setChecked(false);
                    id=2;
                }
            }
        });

    }
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ok:
              if (id==1){
                 getZF();
              }else if (id==2){
                  getWX();
              }

                break;
        }
    }
    //获得支付宝订单号
    private void getZF(){
        HashMap<String,String> map=new HashMap<String, String>();
        map.put("biaoshi","ZF");
        Gson gson=new Gson();
        String params=gson.toJson(map);
        OkGo.post(Constants.URL_BASE+"recharge?action=phonegetCharge")
                .params("params",params)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        System.out.print("支付宝=="+s);
                        String str=s.replace("\"","");
                    }
                });
}
    //获得微信订单号
    private void getWX(){
    HashMap<String,String> map=new HashMap<String, String>();
    map.put("biaoshi","WX");
    Gson gson=new Gson();
    String params=gson.toJson(map);

    OkGo.post(Constants.URL_BASE+"recharge?action=phonegetCharge")
            .params("params",params)
            .execute(new StringCallback() {
                @Override
                public void onSuccess(String s, Call call, Response response) {
                    System.out.print("微信"+s);
                    String str=s.replace("\"","");
                }
            });
}
        }
